/*
 * BikeOptionsPanel.java
 *
 * Copyright 2008 David D. Emory
 * 
 * This file is part of Five Points. See <http://www.fpdev.org> for
 * additional information regarding the project.
 * 
 * Five Points is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Five Points is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Five Points.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.fpdev.apps.rtemaster.gui;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.geom.Line2D;
import java.awt.image.BufferedImage;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import org.fpdev.util.gui.GUIFactory;

/**
 *
 * @author demory
 */
class BikeOptionsPanel extends JPanel {

  private TrianglePanel triangle_;
  private double dist_,  topo_,  type_;
  private JLabel distLabel_,  topoLabel_,  typeLabel_;
  private BarPanel distBar_,  topoBar_,  typeBar_;

  public BikeOptionsPanel() {
    super();


    dist_ = topo_ = type_ = (double) 1 / 3;

    triangle_ = new TrianglePanel();

    distLabel_ = GUIFactory.newLabel("");
    distLabel_.setAlignmentX(JPanel.LEFT_ALIGNMENT);
    topoLabel_ = GUIFactory.newLabel("");
    topoLabel_.setAlignmentX(JPanel.LEFT_ALIGNMENT);
    typeLabel_ = GUIFactory.newLabel("");
    typeLabel_.setAlignmentX(JPanel.LEFT_ALIGNMENT);

    distBar_ = new BarPanel();
    distBar_.setAlignmentX(JPanel.LEFT_ALIGNMENT);
    topoBar_ = new BarPanel();
    topoBar_.setAlignmentX(JPanel.LEFT_ALIGNMENT);
    typeBar_ = new BarPanel();
    typeBar_.setAlignmentX(JPanel.LEFT_ALIGNMENT);

    JPanel labelCol = new JPanel();
    labelCol.setLayout(new BoxLayout(labelCol, BoxLayout.Y_AXIS));

    labelCol.add(distLabel_);
    labelCol.add(Box.createVerticalStrut(2));
    labelCol.add(distBar_);
    labelCol.add(Box.createVerticalStrut(5));

    labelCol.add(topoLabel_);
    labelCol.add(Box.createVerticalStrut(2));
    labelCol.add(topoBar_);
    labelCol.add(Box.createVerticalStrut(5));

    labelCol.add(typeLabel_);
    labelCol.add(Box.createVerticalStrut(2));
    labelCol.add(typeBar_);

    setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
    add(triangle_);
    add(labelCol);

    setBorder(new CompoundBorder(new TitledBorder("Bike Options"), new EmptyBorder(5, 5, 5, 5)));

    updateLabels();
  }

  public void updateLabels() {
    double dist = triangle_.getDist();
    double topo = triangle_.getTopo();
    double type = triangle_.getType();

    dist_ = dist / (dist + topo + type);
    topo_ = topo / (dist + topo + type);
    type_ = type / (dist + topo + type);

    distLabel_.setText("Distance: " + ((int) (100 * dist_)) + "%");
    topoLabel_.setText("Topography: " + ((int) (100 * topo_)) + "%");
    typeLabel_.setText("Fac. Type: " + ((int) (100 * type_)) + "%");

    distBar_.setValue(dist_);
    distBar_.repaint();
    topoBar_.setValue(topo_);
    topoBar_.repaint();
    typeBar_.setValue(type_);
    typeBar_.repaint();
  }

  public double getDist() {
    return dist_;
  }

  public double getTopo() {
    return topo_;
  }

  public double getType() {
    return type_;
  }

  public class TrianglePanel extends JPanel {

    private int x_,  y_,  size_,  alt_;
    private boolean dragging_;
    private BufferedImage bgImg_;

    public TrianglePanel() {
      size_ = 100;
      dragging_ = false;
      double alt = Math.sqrt(3) * size_ / 2;
      alt_ = (int) alt;
      this.setPreferredSize(new Dimension(size_ + 8, alt_ + 2));

      x_ = size_ / 2;
      double y = (size_ / 2) / Math.sqrt(3);
      y_ = alt_ - (int) y;

      initBG();
      /*addMouseListener(new MouseAdapter() {
      @Override
      public void mousePressed(MouseEvent e) {
      int x = e.getX(), y = e.getY();
      if(x > x_-8 && x < x_+8 && y > y_-8 && y < y_+8) {
      System.out.println("start dragging");
      dragging_ = true;
      }
      }
      @Override
      public void mouseReleased(MouseEvent e) {
      dragging_ = false;
      }
      });*/

      addMouseMotionListener(new MouseMotionAdapter() {

        @Override
        public void mouseDragged(MouseEvent e) {
          //System.out.println("mm");
          //if(dragging_) {
          x_ = e.getX();
          y_ = e.getY();
          if (y_ > alt_) {
            y_ = alt_;
          }
          if (y_ < 0) {
            y_ = 0;
          }
          double minx = (size_ / 2) - ((double) y_ / alt_) * (size_ / 2);
          if (x_ < minx) {
            x_ = (int) minx;
          }
          double maxx = (size_ / 2) + ((double) y_ / alt_) * (size_ / 2);
          if (x_ > maxx) {
            x_ = (int) maxx;
          }
          repaint();
          updateLabels();
        //}
        }
      });
    }

    public double getDist() {
      return alt_ - y_;
    }

    public double getTopo() {
      return Line2D.ptLineDist(size_ / 2, 0, size_, alt_, x_, y_);
    }

    public double getType() {
      return Line2D.ptLineDist(size_ / 2, 0, 0, alt_, x_, y_);
    }

    //public int getY() { return y_; }
    private void initBG() {
      bgImg_ = new BufferedImage(size_ + 8, alt_ + 2, BufferedImage.TYPE_INT_RGB);
      Graphics2D g2d = (Graphics2D) bgImg_.getGraphics();
      g2d.setColor(this.getBackground());
      g2d.fillRect(0, 0, size_ + 8, alt_ + 2);

      RenderingHints renderHints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
      renderHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
      g2d.setRenderingHints(renderHints);

      g2d.setColor(Color.white);


      int[] xpts = {0, size_ / 2, size_};
      int[] ypts = {alt_, 0, alt_};

      int[] xpts_closed = {0, size_ / 2, size_, 0};
      int[] ypts_closed = {alt_, 0, alt_, alt_};

      g2d.fillPolygon(xpts, ypts, 3);
      g2d.setColor(Color.gray);
      g2d.setStroke(new BasicStroke(2));
      g2d.drawPolyline(xpts_closed, ypts_closed, 4);

      g2d.drawString("D", size_ / 2 - 4, 20);
      g2d.drawString("T", 14, alt_ - 4);
      g2d.drawString("F", size_ - 20, alt_ - 4);

    }

    @Override
    public void paintComponent(Graphics g) {

      Graphics2D g2d = (Graphics2D) g;

      RenderingHints renderHints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
      renderHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
      g2d.setRenderingHints(renderHints);

      g2d.drawImage(bgImg_, 0, 0, this);

      g2d.setStroke(new BasicStroke(1));
      g2d.setColor(Color.black);
      g2d.drawOval(x_ - 5, y_ - 5, 10, 10);
      g2d.drawLine(x_ - 8, y_, x_ + 8, y_);
      g2d.drawLine(x_, y_ - 8, x_, y_ + 8);
    }
  }

  public class BarPanel extends JPanel {

    double val_;
    int width_;

    public BarPanel() {
      super();
      width_ = 100;
      this.setPreferredSize(new Dimension(width_, 4));
    }

    public void setValue(double val) {
      val_ = val;
    }

    @Override
    public void paintComponent(Graphics g) {
      g.setColor(getBackground());
      g.fillRect(0, 0, width_, 4);

      double x = val_ * width_;
      g.setColor(getBackground());
      g.fillRect(0, 0, width_, 4);
      g.setColor(Color.blue);
      g.fillRect(0, 0, (int) x, 4);
    }
  }
}
